package com.webserver.controller;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class UserController {
    private static File USER_DIR = new File("./users");

    static {
        if (!USER_DIR.exists()) {
            USER_DIR.mkdirs();
        }
    }

    public void showAllUser(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理动态页面...");
        //1.将users目录中的所有用户读取出来
        List<User> userList = new ArrayList<>();
        File[] subs = USER_DIR.listFiles(f -> f.getName().endsWith(".obj"));
        for (int i = 0; i < subs.length; i++) {
            File file = subs[i];
            try (
                    FileInputStream fis = new FileInputStream(file);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                Object obj = ois.readObject();
                //反序列化回来的对象是否为User实例
                if (obj instanceof User) {
                    User user = (User) obj;
                    userList.add(user);

                }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        System.out.println(userList + "============================");


        System.out.println("处理动态页面完毕！！！！！！！！");

        //2.将所有用户拼接到一个html中
        PrintWriter pw = response.getWriter();
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>Title</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>用户列表</h1>");
        pw.println("<table border=\"3\">");
        pw.println("<tr>");
        pw.println("<td>用户名</td>");
        pw.println("<td>密码</td>");
        pw.println("<td>昵称</td>");
        pw.println("<td>年龄</td>");
        pw.println("</tr>");
        for (User user : userList) {
            pw.println("<tr>");
            pw.println("<td>" + user.getUsername() + "</td>");
            pw.println("<td>" + user.getPassword() + "</td>");
            pw.println("<td>" + user.getNickname() + "</td>");
            pw.println("<td>" + user.getAge() + "</td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");
        response.setContentType("text/html");
    }


    public void reg(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理用户注册...");
        //1获取用户在表单上输入的信息
        //通过request的getParameter方法获取,这里参数与页面表单中输入框的名字必须一致
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username + "," + password + "," + nickname + "," + ageStr);
        File userFile = new File(USER_DIR, username + ".obj");
        if (userFile.exists()) {
            File file = new File("./webapps/myweb/have_user.html");
            response.setContentFile(file);
            return;
        }


        int age = Integer.parseInt(ageStr);
        //2.将信息保存
        //用户名.obj
        User user = new User(username, password, nickname, age);
        //3响应客户端注册结果的页面
        File userFile1 = new File(USER_DIR, username + ".obj");
        try (FileOutputStream fos = new FileOutputStream(userFile1);
             ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(user);
            System.out.println("注册成功");


            File file = new File("./webapps/myweb/reg_success.html");
            response.setContentFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("用户注册处理完毕...");
    }

    public void login1(HttpServletResponse response, HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //必要验证
        if (username == null || password == null) {
            File file = new File("./webapps/myweb/login_fail.html");
            response.setContentFile(file);
            return;
        }
        //处理登录
        //定位到登录用户的文件
        File userFile = new File(USER_DIR, username + "obj");
        if (userFile.exists()) {
        }

    }

    public void login2(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理用户登录...");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //必要验证
        if (username == null || password == null) {
            File file = new File("./webapps/myweb/login_info_error.html");
            response.setContentFile(file);
            return;
        }
        //处理登录
        //定位到登录用户的文件
        File userFile = new File(USER_DIR, username + ".obj");
        if (userFile.exists()) {
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(userFile)
                    )
            ) {
                User user = (User) ois.readObject();
                if (user.getPassword().equals(password)) {
                    //登录成功
                    File file = new File("./webapps/myweb/login_success.html");
                    response.setContentFile(file);
                    return;
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        File file = new File("./webapps/myweb/login_fail.html");
        response.setContentFile(file);


        System.out.println("处理用户登录完毕!");
    }


    public void login(HttpServletRequest request, HttpServletResponse response) {
        try {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            FileInputStream userFile = new FileInputStream(USER_DIR + "/" + username + ".obj");
            if (password == null || username == null) {

                File file = new File("./webapps/myweb/login_fail.html");
                response.setContentFile(file);
                return;
            }
            ObjectInputStream objectInputStream = new ObjectInputStream(userFile);
            User user = (User) objectInputStream.readObject();
            if (password.equals(user.getPassword())) {
                File file = new File("./webapps/myweb/login_success.html");
                response.setContentFile(file);
                return;
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println("登录成功");
    }
}

